﻿using JMDict.ObjectModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WindowsStoreSampleApp
{
    public static class JMDicExtensions
    {
        public static IEnumerable<Entry> FindEntries(this Dictionary dic, string searchKeyWord)
        {
            IEnumerable<Entry> selection;

            if (searchKeyWord.ContainsKanji())
            {
                selection = from e in dic.Entries
                            from k in e.Kanjis
                            where k.Reading.StartsWith(searchKeyWord)
                            select e;
            }
            else if (searchKeyWord.ContainsRomaji())
            {
                selection = from e in dic.Entries
                            from s in e.Senses
                            from g in s.gloss
                            from t in g.Text
                            where t.Contains(searchKeyWord)
                            select e;
            }
            else
            {
                selection = from e in dic.Entries
                            from r in e.ReadingElements
                            where r.Reading.StartsWith(searchKeyWord)
                            select e;
            }

            return selection;
        }
    }
}
